package com.gogowan.agroenterprise.contorl.RetrofitUtil;

import android.app.Activity;
import android.content.Context;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;


public class MyRetrofit {
    private static   Retrofit retrofit;
    private static Url_Interface url_interface;
    private static MyRetrofit myRetrofit;
    private static final int DEFAULT_TIMEOUT = 5;
    public static Url_Interface init(Context context,String ServerUrl){
        myRetrofit=new MyRetrofit();
        retrofit=new Retrofit.Builder()
                .baseUrl(ServerUrl)
                .addConverterFactory(StringConverterFactory.create())
                .client(genericClient(context))
                .build();
        url_interface=retrofit.create(Url_Interface.class);
        return url_interface;
    }
    public static MyRetrofit getDownLod(){
        return myRetrofit;
    }
    public static OkHttpClient genericClient(Context context) {
        OkHttpClient httpClient = new OkHttpClient.Builder()
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request original = chain.request();
                        Request.Builder requestBuilder = original.newBuilder()
                                .header("UserId", "1");
                        Request request = requestBuilder.build();
                        return chain.proceed(request);
                    }
                })
                .connectTimeout(300000, TimeUnit.SECONDS)
                .build();

        return httpClient;
    }
    public Retrofit getRetrofit(){
        return retrofit;
    }
}
